using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 学生课表管理控制器
    /// </summary>
    /// <remarks>
    /// 该控制器提供学生课表信息的增删改查功能
    /// 包括分页查询、详情查询、创建、更新和删除操作
    /// </remarks>
    [Authorize]
    //[RequirePermission("student")]
    [ApiController]
    [Route("api/[controller]/[action]")]
    [Obsolete]
    public class StudentScheduleController : ControllerBase
    {
        private readonly IStudentScheduleService _studentScheduleService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="studentScheduleService">学生课表服务</param>
        public StudentScheduleController(IStudentScheduleService studentScheduleService)
        {
            _studentScheduleService = studentScheduleService;
        }

        /// <summary>
        /// 获取学生课表分页列表
        /// </summary>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>学生课表分页列表</returns>
        /// <remarks>
        /// 支持按学生ID、教师课表ID、课表状态等条件筛选
        /// </remarks>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<StudentScheduleDto>>> GetPagedList([FromBody] StudentSchedulePageRequest pageRequest)
        {
            var result = await _studentScheduleService.GetPagedListAsync(pageRequest);
            return ApiResponse<PagedResult<StudentScheduleDto>>.Ok(result);
        }

        /// <summary>
        /// 获取学生课表统计分页列表
        /// </summary>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>学生课表统计分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<StudentScheduleStatisticsDto>>> GetStatisticsPagedList([FromBody] StudentScheduleStatisticsPageRequest pageRequest)
        {
            var result = await _studentScheduleService.GetStatisticsPagedListAsync(pageRequest);
            return ApiResponse<PagedResult<StudentScheduleStatisticsDto>>.Ok(result);
        }

        /// <summary>
        /// 获取学生课表详情
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>学生课表详情</returns>
        [HttpPost]
        public async Task<ApiResponse<StudentScheduleDto>> GetById([FromBody] IdRequest request)
        {
            var schedule = await _studentScheduleService.GetByIdAsync(request.Id);
            return ApiResponse<StudentScheduleDto>.Ok(schedule);
        }

        /// <summary>
        /// 创建学生课表
        /// </summary>
        /// <param name="input">创建学生课表请求参数</param>
        /// <returns>创建后的学生课表信息</returns>
        [HttpPost]
        public async Task<ApiResponse<StudentScheduleDto>> Create([FromBody] CreateStudentScheduleDto input)
        {
            var schedule = await _studentScheduleService.CreateAsync(input);
            return ApiResponse<StudentScheduleDto>.Ok(schedule);
        }
        /// <summary>
        /// 批量创建学生课表
        /// </summary>
        /// <param name="input">批量创建学生课表请求参数</param>
        /// <returns>批量创建后的学生课表信息</returns>
        [HttpPost]
        public async Task<ApiResponse<List<StudentScheduleDto>>> CreateRange([FromBody] CreateStudentScheduleRangeDto input)
        {
            var schedules = await _studentScheduleService.CreateRangeAsync(input);
            return ApiResponse<List<StudentScheduleDto>>.Ok(schedules);
        }

        /// <summary>
        /// 更新学生课表
        /// </summary>
        /// <param name="input">更新学生课表请求参数</param>
        /// <returns>更新后的学生课表信息</returns>
        [HttpPost]
        public async Task<ApiResponse<StudentScheduleDto>> Update([FromBody] UpdateStudentScheduleDto input)
        {
            var schedule = await _studentScheduleService.UpdateAsync(input);
            return ApiResponse<StudentScheduleDto>.Ok(schedule);
        }

        /// <summary>
        /// 删除学生课表
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _studentScheduleService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 根据学生ID和教师课表ID删除学生课表
        /// </summary>
        /// <param name="request">学生ID和教师课表ID请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResponse<object>> DeleteByStudentIdAndTeacherScheduleId([FromBody] DeleteByStudentIdAndTeacherScheduleIdRequest request)
        {
            await _studentScheduleService.DeleteByStudentIdAndTeacherScheduleIdAsync(request.StudentId, request.TeacherScheduleId);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 获取指定学生的所有课表
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>学生课表列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<StudentScheduleDto>>> GetByStudentId([FromBody] IdRequest request)
        {
            var schedules = await _studentScheduleService.GetByStudentIdAsync(request.Id);
            return ApiResponse<List<StudentScheduleDto>>.Ok(schedules);
        }

        /// <summary>
        /// 获取指定日期范围内的学生课表
        /// </summary>
        /// <param name="request">日期范围请求参数</param>
        /// <returns>学生课表列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<StudentScheduleDto>>> GetByDateRange([FromBody] UwinEducation.Application.Dtos.DateRangeRequest request)
        {
            var schedules = await _studentScheduleService.GetByDateRangeAsync(
                request.StudentId,
                request.StartDate,
                request.EndDate);
            return ApiResponse<List<StudentScheduleDto>>.Ok(schedules);
        }

        /// <summary>
        /// 获取教师课表列表
        /// </summary>
        /// <param name="request">教师ID和日期范围请求参数</param>
        /// <returns>教师课表集合</returns>
        [HttpPost]
        public async Task<ApiResponse<List<TeacherScheduleListDto>>> GetTeacherScheduleList([FromBody] TeacherScheduleListRequest request)
        {
            try 
            {

                var schedules = await _studentScheduleService.GetTeacherScheduleListAsync(request);
                return ApiResponse<List<TeacherScheduleListDto>>.Ok(schedules);
            }
            catch (Exception ex)
            {
                return ApiResponse<List<TeacherScheduleListDto>>.Error(ex.Message);
            }
        }

        /// <summary>
        /// 获取学生课表列表
        /// </summary>
        [HttpPost]
        public async Task<ApiResponse<List<StudentScheduleListDto>>> GetStudentScheduleList([FromBody] StudentScheduleListRequest request)
        {
            var schedules = await _studentScheduleService.GetStudentScheduleListAsync(request);
            return ApiResponse<List<StudentScheduleListDto>>.Ok(schedules);
        }

        /// <summary>
        /// 获取教师课表下的所有学生（去重）
        /// </summary>
        /// <param name="request">教师课表ID请求参数</param>
        /// <returns>学生列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<StudentDto>>> GetDistinctStudentsByTeacherScheduleId([FromBody] IdRequest request)
        {
            var students = await _studentScheduleService.GetDistinctStudentsByTeacherScheduleIdAsync(request.Id);
            return ApiResponse<List<StudentDto>>.Ok(students);
            
        }
    }



}